import axios from 'axios'
import qs from 'qs'
import { Message } from 'element-ui'

export const instance = axios.create({
  //   baseURL: process.env.API_ROOT,
  //   baseURL: '/api',
  baseURL: 'http://njt.upload.djtcloud.cn:8002/cross',
  timeout: 5000
})
instance.defaults.headers.post['Content-Type'] =
  'application/x-www-form-urlencoded'
instance.interceptors.request.use(
  config => {
    return config
  },
  err => {
    Message({
      showClose: true,
      message: error,
      type: 'error.data.error.message'
    })
    return Promise.reject(err)
  }
)
instance.interceptors.response.use(
  res => {
    if (res.data && !(res.data.code == '000000')) {
      Message({
        //  饿了么的消息弹窗组件,类似toast
        showClose: true,
        message: res.data.message ? res.data.message : '系统异常',
        type: 'error'
      })
      return Promise.reject(res.data.message)
    }
    if (res.config.responseType == 'blob') {
      console.log('res', res)
    }
    return res
  },
  err => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = '请求错误'
          break

        case 401:
          err.message = '未授权，请登录'
          break

        case 403:
          err.message = '拒绝访问'
          break

        case 404:
          err.message = `请求地址出错 `
          break

        case 408:
          err.message = '请求超时'
          break

        case 500:
          err.message = '服务器内部错误'
          break

        case 501:
          err.message = '服务未实现'
          break

        case 502:
          err.message = '网关错误'
          break

        case 503:
          err.message = '服务不可用'
          break

        case 504:
          err.message = '网关超时'
          break

        case 505:
          err.message = 'HTTP版本不受支持'
          break
        default:
      }
    }
    Message({
      showClose: true,
      message: err.message,
      type: 'error'
    })
    return Promise.reject(err)
  }
)

export const get = (url, params) => {
  return new Promise((resolve, reject) => {
    let Url
    if (params === null || params === undefined) {
      Url = url
    } else {
      Url = url + '?' + qs.stringify(params)
    }
    instance.get(Url).then(res => resolve(res.data), err => reject(err))
  })
}

export const post = (url, params) => {
  console.log(params)
  let Url = url + '?' + qs.stringify(params)
  return new Promise((resolve, reject) => {
    instance.post(Url).then(res => resolve(res.data), err => reject(err))
  })
}

export const put = (url, params) => {
  console.log('params', params)
  return new Promise((resolve, reject) => {
    instance
      .post(url, params)
      .then(res => resolve(res.data), err => reject(err))
  })
}
export const uploadFile = (url, params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(url, params, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then(res => resolve(res.data), err => reject(err))
  })
}

export const del = (url, params) => {
  let Url = url + '?' + qs.stringify(params)
  return new Promise((resolve, reject) => {
    instance.delete(Url).then(res => resolve(res.data), err => reject(err))
  })
}

export const download = url => {
  return new Promise((resolve, reject) => {
    instance.get(url, { responseType: 'blob' }).then(
      res => {
        console.log(res)
        resolve(res)
      },
      err => {
        console.log('err', err)
        reject(err)
      }
    )
  })
}
